package com.hefu.virtuallife.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.IdRes;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dz.utlis.AndroidUtils;
import com.dz.utlis.JavaUtils;
import com.dz.utlis.ToastUtil;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.hefu.virtuallife.App;
import com.yanzhenjie.nohttp.rest.Response;

import java.util.ArrayList;
import java.util.List;


/**
 * creat_user: zhengzaihong
 * Email:1096877329@qq.com
 * creat_date: 2018/5/28
 * creat_time: 18:05
 * describe
 **/
public class Tools extends JavaUtils {

    public static boolean isJson(String json) {
        boolean isjson = true;
        if (isdebug)
            outPrint("输出json: " + json);
        if (null == json || "".equals(json)) {
            isjson = false;
            return isjson;
        }
        try {
            new JsonParser().parse(json);
            return isjson;
        } catch (JsonParseException e) {
            isjson = false;
            return isjson;
        }
    }

    public synchronized static boolean getstatus(JSONObject json) {
        boolean code_ok = false;
        if (json.getInteger("code") == 200) {
            code_ok = true;
        } else if (json.getInteger("code") == 211) {

       /*     SPUser.getUser(App.get()).clearUserInfo();
            App.get().finishActivity();
            Intent intent = new Intent(App.get(), AccountActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ToastUtil.showCenterTextToast(App.get(), "请重新登录!");
            App.get().startActivity(intent);
            code_ok = false;*/
        }
        return code_ok;
    }


    public static void saveUserInfo(JSONObject js) {
        SPUser.getUser(App.get())
                .setPhone(js.getString("phone"))
                .setUid(js.getString("id"))
                .setUserName(js.getString("nick_name"))
                .setHead(js.getString("head_pic"))
                .setbalanceL(js.getString("balance"))
                .setintegral(js.getString("integral"))
                .setannual_rate(js.getString("annual_rate"))
                .setprincipal(js.getString("principal"))
                .setalipay_account(js.getString("alipay_account"))
                .setalipay_user_name(js.getString("alipay_user_name"))
                .setfriend_code(js.getString("friend_code"));
    }

    public static boolean ismatchPass(String str) {
        boolean flag = false;
        if (!TextUtils.isEmpty(str) && str.length() > 5 && str.length() < 16) {
            flag = true;
        }
        return flag;
    }

    /**
     * 可对字体响应点击事件
     */
    public static void setTextColorAndClick(final TextView textView, String content, int startPosition, int endPosition, final String colors) {
        SpannableString spanString = new SpannableString(content);
        //将这个Span应用于指定范围的字体
        spanString.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.parseColor(colors));//设置文本颜色
                ds.setUnderlineText(true);      //设置下划线
            }

            @Override
            public void onClick(View widget) {
                if (null != onClickTextListener) {
                    onClickTextListener.onclick(widget);
                }
            }
        }, startPosition, endPosition, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        //设置给view显示出来
        textView.setText(spanString);
        textView.setHighlightColor(Color.TRANSPARENT);//去掉点击后出现的高亮显示
        textView.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }



    /**
     * 测量view 的宽度高度
     */
    public static int[] measureView(View view) {
        int[] Measure = new int[2];
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        int height = view.getMeasuredHeight();
        int width = view.getMeasuredWidth();
        Measure[0] = width;
        Measure[1] = height;
        return Measure;

    }


    /**
     * 验证手机号
     */
    public static boolean isMobileNO(String mobiles) {
        return (!TextUtils.isEmpty(mobiles) && mobiles.length() == 11);
    }


    /**
     * 用于点击文字监听器
     */
    public static OnClickTextListener onClickTextListener;


    public interface OnClickTextListener {
        void onclick(View widget);
    }
}
